<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    NAME: Export as outline v2_35.xsl
    LAST MODIFIED: 6/27/06
    
    PURPOSE: To transform an InfoML 0.871 document into a text file formatted as
        a rudimentary outline. This file is to be used with Infocard Desktop's
        Export function. Works with version 060609 of Infocard Desktop.
    
    BUGS:
    
    * none known; probably crashes if input file is invalid
    
    NOTES:
	
    * This transformation was designed to work with XSLT V1.0. It is *NOT*
    namespace-aware.
	
    * The only difference between this file (v2_35) and the previous version
	(v2_3) is the removal of namespace information from the previous
	version to this one. The current version of Infocard Desktop
	seems to require this.
	
    * This transformation assumes that the first infoml element in the file
    contains nothing but a list of pointers to the top-level infocards. This
    is part of the design that enables an infocard file to represent either a
    list of infocards or an outline.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<!--  -->
	<xsl:output media-type="text" omit-xml-declaration="yes"/>
	<!--  -->
	<xsl:template match="infomlFile">
		<xsl:apply-templates/>
	</xsl:template>
	<!--  -->
	<!--    <xsl:template match="*" priority="-10">
        <xsl:text>&#10;</xsl:text>
        <xsl:text>CONTENT UNACCOUNTED FOR:</xsl:text>
        <xsl:value-of select="name()"/>
    </xsl:template>
-->
	<!--  -->
	<!-- This forces processor to ignore all infoml elements (except first one, as
		defined by the next template (below) -->
	<xsl:template match="infoml"/>
	<!--  -->
	<!-- This forces processor to process first infoml element. It begins by calling
		the "recurse" template on the root infoml element. -->
	<xsl:template match="//infoml[1]">
		<xsl:call-template name="recurse">
			<xsl:with-param name="node" select="."/>
			<xsl:with-param name="indentString" select="''"/>
		</xsl:call-template>
	</xsl:template>
	<!--  -->
	<!-- the "recurse" template -->
	<!-- Arguments: $node = node to be printed -->
	<!--     $indentString = contains spaces only; used to create indentation -->
	<xsl:template name="recurse">
		<xsl:param name="node"/>
		<xsl:param name="indentString"/>
		<!--  -->
		<!-- indentAmount controls the size of the indentation -->
		<xsl:variable name="indentAmount" select="'    '"/>
		<!--  -->
		<xsl:choose>
			<!-- CASE 1: This is a leaf node; print it, with indentation -->
			<xsl:when test="$node//title">
				<!-- (1) -->
				<xsl:value-of select="$indentString"/>
				<xsl:text>--</xsl:text>
				<xsl:value-of select="$node//title/text()"/>
				<xsl:text>&#10;</xsl:text>
			</xsl:when>
			<!--  -->
			<!-- CASE 2: This is a group node, with parent and child nodes -->
			<xsl:otherwise>
				<!-- Follow parentPtr to print the node it points to. -->
				<!-- parentNode is the actual infoml element to be printed. -->
				<xsl:for-each select="$node//parentPtr">
					<xsl:variable name="idOfParentNode" select="string (@targetId)"/>
					<xsl:variable name="parentNode" select="//infoml [cardId = $idOfParentNode]"/>
					<xsl:call-template name="recurse">
						<xsl:with-param name="node" select="$parentNode"/>
						<xsl:with-param name="indentString" select="$indentString"/>
					</xsl:call-template>
				</xsl:for-each>
				<!-- Follow ptr to print the (child) node, indented to the right. -->
				<!-- childNode is the actual infoml element to be processed; recursion is necessary
				because childNode may itself be a parent/child pointer node. -->
				<xsl:for-each select="$node//ptr">
					<xsl:variable name="idOfChildNode" select="string (@targetId)"/>
					<xsl:variable name="childNode" select="//infoml [cardId = $idOfChildNode]"/>
					<xsl:variable name="pointersOnlyNode" select="/infomlFile/infoml [1]"/>
					<xsl:choose>
						<xsl:when test="$node = $pointersOnlyNode">
							<xsl:call-template name="recurse">
								<xsl:with-param name="node" select="$childNode"/>
								<xsl:with-param name="indentString"
									select="$indentString"/>
							</xsl:call-template>
						</xsl:when>
						<xsl:otherwise>
							<xsl:call-template name="recurse">
								<xsl:with-param name="node" select="$childNode"/>
								<xsl:with-param name="indentString"
									select="concat($indentString, $indentAmount)"/>
							</xsl:call-template>
						</xsl:otherwise>
					</xsl:choose>

				</xsl:for-each>
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>
	<!--  -->
	<!--  -->
	<!-- This template "gobbles up" all text elements found between elements (mostly Return characters). -->
	<xsl:template match="text()"/>
	<!--  -->


</xsl:stylesheet>
